package com.magical.controller;

import com.magical.entity.Files;
import com.magical.entity.RestJson;
import com.magical.entity.Shares;
import com.magical.entity.User;
import com.magical.service.FilesService;
import com.magical.service.SharesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

import static com.magical.util.MyUtil.generateFileId;


@Controller
@RequestMapping("/shares")
public class SharesController{

    @Autowired
    private FilesService filesService;

    @Autowired
    private SharesService sharesService;

    //显示所有链接
    @RequestMapping("/selectAllShares")
    @ResponseBody
    public List<Shares> selectAllShares(HttpServletRequest request){
        HttpSession session = request.getSession();
        int userId = ((User)session.getAttribute("loginUser")).getId();
        return sharesService.showAllShares(userId);
    }

    //显示所有链接
    @RequestMapping("/ADselectAllShares")
    @ResponseBody
    public RestJson<List<Shares>> ADselectAllShares(HttpServletRequest request){
        HttpSession session = request.getSession();
        int userId=Integer.parseInt(request.getParameter("userId"));
        List<Shares> shares= sharesService.showAllShares(userId);

        RestJson<List<Shares>> result=new RestJson<>();
        if (shares == null){
            result.setCode(-1);
            result.setMessage("暂无分享");
            return result;
        }
        else {
            result.setData(shares);
            return result;
        }
    }


    //根据shareId 删除该链接
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(HttpServletRequest request){
        String shareId =request.getParameter("shareId");
        if(sharesService.delete(shareId)==1){
            System.out.println("success");
            return"success";
        }else{
            System.out.println("failed");
            return "failed";
        }
    }

    //添加分享
    @RequestMapping("/addShare")
    @ResponseBody
    public String addShare(HttpServletRequest request) {
        HttpSession session = request.getSession();
        int userId = ((User)session.getAttribute("loginUser")).getId();
        String[] ids = request.getParameter("ids").split(",");
        try {
            for (int i = 0; i < ids.length; i++) {
                Files file = filesService.findById(ids[i]);
                Shares shares = new Shares();
                shares.setFileId(ids[i]);
                String id=generateFileId(file.getFileName());
                String url = request.getServerName() + "/share.jsp?id=" + id;
                shares.setShareUrl(url);
                shares.setShareId(id);
                shares.setUserId(userId);
                sharesService.addShare(shares);
            }
            return "success";
        }catch (Exception e){
            return "error";
        }
    }

    //显示分享文件
    @RequestMapping("/showShare")
    @ResponseBody
    public Shares showShare(HttpServletRequest request){
        String shareId =request.getParameter("shareId");
        Shares share=sharesService.showShare(shareId);
        return  share;
    }
}